const result = require("./handle")

class checking{
    constructor(ctx,...obj){
        this.ctx = ctx
        this.obj = obj
    }
    Errunder(){
        let bvc = this.obj.indexOf(undefined)
        if (bvc != -1) {
            throw new result('参数填写错误',400)
        }
    }
    //check phonenumber
    checkPhone(mobil,n){
        let num = /^1[3456789]\d{9}$/
        if (!num.test(this.obj[n])) {
            throw new result(mobil,202)
        }
    }
    //check password
    checkPassword(m,n){
        let pas = /^(\w){6,20}$/
        if (!pas.test(this.obj[n])) {
            throw new result(m,203)
            
        }
    }
    //check arr
    checkArr(list,num){
        if (JSON.parse(this.obj[num]).length === 0) {
            throw new result(list,202)
        }
    }
    //isnull
    Parameter(list){
        let bvc = this.obj.indexOf('')
        if (bvc != -1) {
            throw new result(list[bvc],202)
        }
    }
    //isBlank
    Blank(list){
        let vbn = this.obj.filter(item =>{
            return item.split('  ').join('').length === 0
        })
        let bvc = this.obj.indexOf(vbn[0])
        if (bvc != -1) {
            throw new result(list[bvc],202)
        }
    }
}
class regCheck extends checking{
    start(){
        super.Errunder()
        super.checkPhone('请输入正确手机号',0)
        super.checkPassword('请输入正确密码',1)
    }
}
//检验商家信息上传
class shopInfor extends checking{
    start(){
        let arr = ['请输入店铺名称','请输入地址','请输入头像']
        super.Errunder()
        super.Parameter(arr)
        super.Blank(arr)
        super.checkArr('请输入头像',2)
    }
}
//校验菜品类目
class categoryCheck extends checking{
    start(){
        let arr = ['请输入菜品类目']
        super.Errunder()
        super.Parameter(arr)
        super.Blank(arr)
    }
}
class unitCheck extends checking{
    start(){
        let arr = ['请输入菜品单位']
        super.Errunder()
        super.Parameter(arr)
        super.Blank(arr)
    }
}
class dishCheck extends checking{
    start(){
        let arr = ['请输入菜品类目','请输入菜品名称','请输入价格','请选择单位','请上传图片','请上传cid']
        super.Errunder()
        super.Parameter(arr)
        super.Blank(arr)
        super.checkArr('请输入图片',4)
    }
}
class tableCheck extends checking{
    start(){
        let arr = ['请输入桌号']
        super.Errunder()
        super.Parameter(arr)
        super.Blank(arr)
    }
}
module.exports = {regCheck,shopInfor,categoryCheck,unitCheck,dishCheck,tableCheck}